package com.zhiyun.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.Base64;
import com.gzit.utils.DateUtil;
import com.gzit.utils.MD5;

public final class YYCallUtils {

	public static synchronized String encryptPwd(String phoneNo, String password) {
		try {

			byte[] desed = DESUtils
					.encrypt(
							(MD5.toMD5(password) + "("
									+ System.currentTimeMillis() + ")")
									.getBytes("UTF-8"), phoneNo
									+ "zhiyunyycall");
			String enced = Base64.encode(desed);
			return enced;
		} catch (Exception e) {
			e.printStackTrace();
			String error = e.getMessage();
			Log.e("YYCallUtils", error);
			return password;
		}

	}

	public static String queryNameByPhoneNo(String phoneNo) {

		if (phoneNo == null) {
			return null;
		}

		Cursor cursor = null;
		try {
			cursor = AndroidUtils
					.appCtx()
					.getContentResolver()
					.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
							ContactsContract.CommonDataKinds.Phone.NUMBER
									+ "='" + phoneNo + "'", null, null);
			if (null == cursor || cursor.getCount() < 1
					|| !cursor.moveToFirst()) {
				return null;
			}

			return cursor
					.getString(cursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
		} finally {

			if (cursor != null) {
				cursor.close();
			}

		}

	}

	public static final DateFormat DATETIME_FORMAT = new SimpleDateFormat(
			"MM/dd HH:mm:ss");

	public static String getDateTimeDesc(Date date) {
		if (DateUtil.isToday(date)) {
			return "今天 " + DateUtil.time2String(date);
		}

		return DATETIME_FORMAT.format(date);

	}
}
